#include<bits/stdc++.h>
#define int long long
#define ls(x) ((x)*2)
#define rs(x) ((x)*2+1)
#define Debug(...) fprintf(stderr, __VA_ARGS__)
#define For(i,a,b) for(int i=a,i##end=b;i<=i##end;i++)
#define Rof(i,a,b) for(int i=a,i##end=b;i>=i##end;i--)
#define rep(i,  b) for(int i=1,i##end=b;i<=i##end;i++)
using namespace std;
const int N=1e6+5,Mod=998244353;
//char buf[(1<<21)+5],*p1,*p2;
//#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
void chmx(int &x,int y){(x<y)&&(x=y);}
void chmn(int &x,int y){(x>y)&&(x=y);}
typedef __int128_t i128;
i128 _base=1;
inline int mol(int x){return x-Mod*(_base*x>>64);}
inline void Add(int &x,int y){x=mol(x+y+Mod);}
inline int read(){
	int f=0,x=0;
	char ch=getchar();
	while(!isdigit(ch)){f|=(ch=='-');ch=getchar();}
	while(isdigit(ch)){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}
	return f?-x:x;
}
int n,k;
int a[N];
set<int>S[105];
map<int,int>mp,ss[105];
int s,t=10000,tot=1;
int maxn,minn;
int dis[N],head[N],fv[N],pre[N],last[N];
bool vis[N];
queue<int>q;
struct XXX{
	int next,to,pay,val;
}e[N<<2];
void add(int u,int v,int w,int p) {
	e[++tot].next=head[u];
	e[tot].to=v;
	e[tot].val=w;
	e[tot].pay=p;
	head[u]=tot;
	
	e[++tot].next=head[v];
	e[tot].to=u;
	e[tot].val=0;
	e[tot].pay=-p;
	head[v]=tot;
}
bool spfa(){
	For(i,s,t)dis[i]=fv[i]=1e16,vis[i]=0;
	q.push(s);
	dis[s]=0;
	vis[s]=1;
	pre[t]=-1;
	while(!q.empty()) {
		int u=q.front();
		q.pop();
		vis[u]=0;
		for(int i=head[u];i;i=e[i].next) {
			if(!e[i].val) continue;
			int v=e[i].to;
			if(dis[v]>dis[u]+e[i].pay) {
				dis[v]=dis[u]+e[i].pay;
				pre[v]=u;
				last[v]=i;
				fv[v]=min(fv[u],e[i].val);
				if(!vis[v]){
					vis[v]=1;
					q.push(v);
				}
			}
		}
	}
	return pre[t]!=-1;
}
void EK(){
	while(spfa()){
		int x=t;
		maxn+=fv[t];
		minn+=dis[t]*fv[t];
		while(x!=s){
			e[last[x]].val-=fv[t];
			e[last[x]^1].val+=fv[t];
			x=pre[x];
		}
	}
}
int f[N];
void dfs(int x,int p,int lst,int yu,int mx){
    if(p==0){
        f[x]=min(f[x],f[yu]+(a[lst+1]-a[mx+1]));
        return;
    }
    for(int i=lst+1;i<n;i++){
        if(!(x&(1<<i))&&a[i+1]!=a[lst+1]){
            dfs(x+(1<<i),p-1,i,yu,mx);
        }
    }
}
signed main(){
	_base=(_base<<64)/Mod;
	freopen("diyiti.in","r",stdin);
	freopen("diyiti.out","w",stdout);
	n=read(),k=read();
	For(i,1,n)a[i]=read(),mp[a[i]]++;
	sort(a+1,a+n+1);
	For(i,1,n){if(mp[a[i]]>k){puts("-1");return 0;}}
    if(n<=16){
        memset(f,0x3f,sizeof(f));
        f[0]=0;
        for(int i=0;i<(1<<n);i++){
            int t=0;
            for(int j=0;j<n;j++){
                if((i&(1<<j))) t+=(1<<(n-j-1));
            }
            if(f[t]==f[(1<<n)+1]) continue;
            int p=0;
            int gg=1e16;
            for(int j=0;j<n;j++){
                if((t&(1<<j))) p++;
                else gg=min(gg,j);
            }
            if(p%(n/k)!=0) continue;
            dfs(t+(1<<gg),n/k-1,gg,t,gg);
        }
        if(f[(1<<n)-1]>10000000000000) puts("-1");
        else cout<<f[(1<<n)-1];
        return 0;
    }
    int ans2=1e16;
	for(auto v:mp){
		int x=v.first,y=v.second;
		//~ cout<<x<<" "<<y<<endl;
		int fi=k+1;
		For(i,1,k){
			if(y==0) break;
			if((int)S[i].size()<n/k){
				fi=min(fi,i);
				y--;
				S[i].insert(-x);
				ss[i][x]=1;
			}
		}
		//~ cout<<y<<" "<<fi<<endl;
		if(y){
			//~ cout<<"safsaf";
			Rof(i,fi-1,1){
				//~ cout<<i;
				if(y==0) break;
				S[i].insert(-x);
				y--;
				ss[i][x]=1;
			}
		}
		For(i,1,k){
			//~ cout<<s[i].size()<<" ";
			while((int)S[i].size()>n/k){
				for(auto v:S[i]){
					if(-v==x) continue;
					bool flag=0;
					For(j,i+1,k){
						if(ss[j][-v])continue;
						else{
							S[j].insert(v);
							ss[j][-v]=1;
							flag=1;
							S[i].erase(v);
							ss[i][-v]=0;
							break;
						}
					}
					if(flag) break;
				}
			}
		}
	}
	int ans1=0;
	For(i,1,k){
		int x=*S[i].begin(),y=*S[i].rbegin();
		ans1+=abs(y-x);
		//~ cout<<s[i].size()<<endl;
		//~ for(auto v:s[i]) cout<<v<<" ";
		//~ puts("");
	}
	int ppp=0;
	for(auto v:mp){
		int x=v.first,y=v.second;
		add(s,++ppp,y,0);
		For(i,1,k){
			int T=100+i*3;
			add(ppp,T+1,1,0);
			add(ppp,T+2,1,-x);
			add(ppp,T+3,1,x);
		}
	}
	For(i,1,k){
		int T=100+i*3;
		add(T+1,t,n/k-2,0);
		add(T+2,t,1,0);
		add(T+3,t,1,0);
	}
	EK();
	if(maxn!=n){puts("-1");return 0;}
	printf("%lld\n",min(min(ans1,ans2),abs(minn)));
#ifdef LOCAL
    Debug("\nMy Time: %.3lfms\n", (double)clock() / CLOCKS_PER_SEC);
#endif
	return 0;
}
